package com.typhoon.spring_shiro.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.typhoon.spring_shiro.dto.RoleDto;
import com.typhoon.spring_shiro.entity.Permission;
import com.typhoon.spring_shiro.utils.CollectionUtils;

@Repository("permissionDao")
public class PermissionDao extends BaseDao implements ICommonDao<Permission, Long> {
	
	private static final String TABLE_PERMISSION = " Permission ";
	
	private static final String TABLE_ROLE_PERMISION = " RolePermission ";
	
	@Resource
	private JdbcTemplate financialJdbc;

	@Override
	public Permission queryByPrimaryKey(Long primaryKey) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByPrimaryKey(Long primaryKey) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Permission get(Object paramObject) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Permission> list(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Permission> list(Object paramObject) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Permission paramE) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long count(Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long count(Object paramObject) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Permission save(Permission paramE) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Permission> listAllPermission(List<RoleDto> roles) {
		Assert.state(CollectionUtils.isNotEmpty(roles));
		StringBuilder sql = new StringBuilder();
		List<Long> params = new ArrayList<>(roles.size());
		sql.append(select);
		sql.append(" p.* ");
		sql.append(from);
		sql.append(TABLE_PERMISSION).append(" p LEFT JOIN ");
		sql.append(TABLE_ROLE_PERMISION).append(" rp on rp.permissionId = p.id  ");
		sql.append(where);
		sql.append("  rp.roleId in (  ");
		RoleDto role = null;
		for(int i = 0,size = roles.size(); i < size;i ++) {
			role = roles.get(i);
			params.add(role.getId());
			sql.append(" ? ");
			if(i < size - 1)
				sql.append(",");
		}
		sql.append("  )  ");
		return this.queryList(financialJdbc, Permission.class, sql.toString(), params.toArray());
	}

}
